<?php
namespace app\admin\controller;
use think\Controller;

class Deal extends Controller  //控制器层
{
    //构造函数 私有属性
	private $obj;
	public function _initialize(){
		$this->obj = model("Deal");
		}

	public function test()
	{
		$res = $this->obj->get(9);
		$aa = $res['description'];
		$bb = strip_tags($res['description']);
		echo $aa,$bb;
		return $this->fetch('',[
			'aa' => $aa,
			'bb' => $bb,
		]);
	}
		
	
	//商户列表
	public function index()
    {
        $data = input('get.');
		$sdata = [];
		
		if( !empty($data['start_time']) && !empty($data['end_time']) 
		              && strtotime($data['end_time']) > strtotime($data['start_time']) ){
						   
						  
				$sdata['create_time']=[
				  ['gt', strtotime($data['start_time'])],
				   ['lt', strtotime($data['end_time'])],
				];	  
			}
		
		if(!empty($data['category_id'])){
			  $sdata['category_id'] = $data['category_id'];
			}
		if(!empty($data['city_id'])){
			  $sdata['city_id'] = $data['city_id'];
			}

		if(!empty($data['name'])){
			  $sdata['name'] = ['like', '%'.$data['name'].'%'];
			}
		
		$deals = $this->obj->getDealsByStatus($sdata, 1);

		$categoryArrs = $cityArrs =[];
		$categorys = model("Category")->getNormalCategorysByParentId();
		foreach($categorys as $category){
			$categoryArrs[$category->id] = $category->name;
			}
		
		$citys = model('City')->getNormalCitys();
		foreach($citys as $city){
			$cityArrs[$city->id] = $city->name;
			}
		
		return $this->fetch('', [
		    'categorys' => $categorys,
			'citys' => $citys,
			'deals' => $deals,
			'category_id' => empty($data['category_id']) ? '' : $data['category_id'],
			'city_id' => empty($data['city_id']) ? '' : $data['city_id'],
			'name' => empty($data['name']) ? '' : $data['name'],
			'start_time' => empty($data['start_time']) ? '' : $data['start_time'],
			'end_time' => empty($data['end_time']) ? '' : $data['end_time'],
			'categoryArrs' => $categoryArrs,
			'cityArrs' => $cityArrs,

		]);

	}
	
	

	public function deallist()
    {
        $data = input('get.');
		$sdata = [];
		
		if( !empty($data['start_time']) && !empty($data['end_time']) 
		              && strtotime($data['end_time']) > strtotime($data['start_time']) ){
						   
						  
				$sdata['create_time']=[
				  ['gt', strtotime($data['start_time'])],
				   ['lt', strtotime($data['end_time'])],
				];	  
			}
		
		if(!empty($data['category_id'])){
			  $sdata['category_id'] = $data['category_id'];
			}
		if(!empty($data['city_id'])){
			  $sdata['city_id'] = $data['city_id'];
			}

		if(!empty($data['name'])){
			  $sdata['name'] = ['like', '%'.$data['name'].'%'];
			}
		
		$deals = $this->obj->getDealsByStatus($sdata, 0);

		$categoryArrs = $cityArrs =[];
		$categorys = model("Category")->getNormalCategorysByParentId();
		foreach($categorys as $category){
			$categoryArrs[$category->id] = $category->name;
			}
		
		$citys = model('City')->getNormalCitys();
		foreach($citys as $city){
			$cityArrs[$city->id] = $city->name;
			}
		
		return $this->fetch('', [
		    'categorys' => $categorys,
			'citys' => $citys,
			'deals' => $deals,
			'category_id' => empty($data['category_id']) ? '' : $data['category_id'],
			'city_id' => empty($data['city_id']) ? '' : $data['city_id'],
			'name' => empty($data['name']) ? '' : $data['name'],
			'start_time' => empty($data['start_time']) ? '' : $data['start_time'],
			'end_time' => empty($data['end_time']) ? '' : $data['end_time'],
			'categoryArrs' => $categoryArrs,
			'cityArrs' => $cityArrs,

		]);

	}	
		
	
 public function status(){
	     $data = input('get.');
		 $res = $this->obj->save(['status'=>$data['status']], 
		     ['id'=>$data['id']]);
	    //halt($res);
		 if($res){
			 $this->success('状态更新成功');
			 }else{
				$this->error('状态更新失败');
				 }
	 
	 }	
	
	
	
	public function detail(){
	     
		 $data = input('get.');
		 //halt($data);
		 $deal = $this->obj->get($data['id']);
		 
		// halt($deal);
		 //获取城市数据
		 $citys = model('City')->getNormalCitysByParentId();
		 $city = model('City')->get($deal['city_id']);
		 $secitys = model('City')->getNormalCitysByParentId($city['parent_id']);

		//获取分类数据
		$categorys = model('Category')->getNormalCategorysByParentId(0);
		$secategorys = model('Category')->getNormalCategorysByParentId($deal['category_id']);
		$secats = explode(',', $deal['se_category_id']);
		
		//门店数据
		$locations = model('BisLocation')->getNormalLocationByBisId($deal['bis_id']); // halt($locations);
		$los = explode(',', $deal['location_ids']);
		
		
		 
		 return $this->fetch('',[
		     'deal' => $deal,
			 'secitys' => $secitys,
			 'citys' => $citys,
			 'categorys' => $categorys,
			 'secategorys' => $secategorys,
			 'city' => $city,
			 'secats' => $secats,
			 'locations' => $locations,
			 'los' => $los,
			 'bis_id' => isset($data['bis_id']) ? $data['bis_id'] : '',
		 ]);
	 
	 }
	
	
	
	
	
	
	
	
	
	
}
?>